define(function(require){
    'use strict';

    var _ = require('underscore'),
        JSON = window.JSON,
        localStorage = window.localStorage;

    var Storage = function(name, options){
        var id = options.id;
        this.name = 'mp-' + window._global.bid_id + '-adm-' +  window._global.user_id + '-';
        this.name += id ? name + '-' + id : name;
    };

    _.extend(Storage.prototype, {

        set: function(key, value) {
            if(!localStorage) return;

            value = JSON.stringify(value);

            try{
                localStorage.setItem(key, value);
            } catch(e){
                // LocalStorage只能存储5M的数据，如果超了，就会报QuotaExceededError异常
                // 这时，我们需要清除存储区域
                if(e.name === 'QuotaExceededError'){
                    localStorage.clear();
                    localStorage.setItem(key, value);
                }
            }
        },

        get: function(key) {
            if(!localStorage) return;
            return JSON.parse(localStorage.getItem(key));
        },

        remove: function(key) {
            if(!localStorage) return;
            localStorage.removeItem(key);
        },

        setItem: function(data){
            this.set(this.name, data);
        },

        getItem: function(){
            return this.get(this.name);
        },

        removeItem: function(){
            this.remove(this.name);
        }

    });

    return Storage;
});
